package com.cy.store.service.impl;

import com.cy.store.entity.Cart;
import com.cy.store.entity.Product;
import com.cy.store.mapper.CartMapper;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.ICartService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.CartNotFoundException;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UpdateException;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class ICartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
        //查询当前要添加的商品在购物车中是否已存在
        Date date = new Date();
        Cart result = cartMapper.findByUidAndPid(uid, pid);
        if (result == null){
            //商品还未被添加过
            Cart cart = new Cart();
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);  //前端把数据加好再传递过来
            Product product = productMapper.findById(pid);
            cart.setPrice(product.getPrice());
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            cart.setModifiedUser(username);
            cart.setModifiedTime(date);
            Integer row = cartMapper.insert(cart);
            if (row != 1){
                throw new InsertException("插入数据时产生未知的异常!");
            }
        }else {
            //商品在购物车中已存在
            Integer number = result.getNum() + amount;
            Integer row = cartMapper.updateNumByCid(result.getCid(), number, username, date);
            if (row != 1){
                throw new UpdateException("更新数据时产生未知异常！");
            }
        }
    }

    @Override
    public List<CartVo> getVOByUid(Integer uid) {
        List<CartVo> voByUid = cartMapper.findVOByUid(uid);
        return voByUid;
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if (result == null){
            throw new CartNotFoundException("数据不存在！");
        }
        if (!(result.getUid().equals(uid))){
            throw new AccessDeniedException("数据非法访问！");
        }
        Integer num = result.getNum() + 1;
        Integer res = cartMapper.updateNumByCid(cid, num, username, new Date());
        if (res != 1){
            throw new UpdateException("更新失败!");
        }
        return num;
    }

    @Override
    public List<CartVo> getVOByCids(Integer[] cids, Integer uid) {
        List<CartVo> carts = cartMapper.findVOByCid(cids);
        Iterator<CartVo> it = carts.iterator();
        while (it.hasNext()){
            CartVo cartVo = it.next();
            if (!cartVo.getUid().equals(uid)){
                //当前数据不属于当前用户，移除数据
                carts.remove(cartVo);
            }
        }
        return carts;
    }
}
